<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML5 文件上传示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .upload-container {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-weight: 300;
        }

        .upload-area {
            border: 3px dashed #ddd;
            border-radius: 15px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 20px;
        }

        .upload-area:hover, .upload-area.dragover {
            border-color: #667eea;
            background-color: #f8f9ff;
        }

        .upload-area i {
            font-size: 48px;
            color: #667eea;
            margin-bottom: 15px;
        }

        .upload-text {
            color: #666;
            margin-bottom: 15px;
        }

        .file-input {
            display: none;
        }

        .browse-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 16px;
            transition: transform 0.2s ease;
        }

        .browse-btn:hover {
            transform: translateY(-2px);
        }

        .file-list {
            margin-top: 20px;
        }

        .file-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
            margin-bottom: 10px;
        }

        .file-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .file-icon {
            width: 40px;
            height: 40px;
            background: #667eea;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
        }

        .file-details {
            flex: 1;
        }

        .file-name {
            font-weight: 500;
            color: #333;
            margin-bottom: 5px;
        }

        .file-size {
            color: #666;
            font-size: 12px;
        }

        .progress-bar {
            width: 100%;
            height: 6px;
            background: #e9ecef;
            border-radius: 3px;
            overflow: hidden;
            margin-top: 10px;
        }

        .progress {
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            width: 0%;
            transition: width 0.3s ease;
        }

        .upload-btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            cursor: pointer;
            margin-top: 20px;
            transition: all 0.3s ease;
        }

        .upload-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .upload-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .preview-container {
            margin-top: 20px;
        }

        .preview-title {
            margin-bottom: 15px;
            color: #333;
        }

        .preview-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
            gap: 10px;
            margin-top: 15px;
        }

        .preview-item {
            position: relative;
            border-radius: 8px;
            overflow: hidden;
        }

        .preview-image {
            width: 100%;
            height: 100px;
            object-fit: cover;
        }

        .remove-btn {
            position: absolute;
            top: 5px;
            right: 5px;
            background: rgba(255, 255, 255, 0.9);
            border: none;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            cursor: pointer;
            font-size: 12px;
        }

        @media (max-width: 768px) {
            .upload-container {
                padding: 20px;
            }
            
            .upload-area {
                padding: 20px;
            }
        }
    </style>
</head>
<body>
    <div class="upload-container">
        <h1>📁 文件上传</h1>
        
        <div class="upload-area" id="dropArea">
            <div class="upload-icon">📁</div>
            <p class="upload-text">拖放文件到此处或点击选择文件</p>
            <button class="browse-btn" onclick="onClickUpload(event)">
                浏览文件
            </button>
            <input type="file" id="fileInput" class="file-input" name="imgfile" multiple 
                   accept="image/*,.pdf,.doc,.docx,.zip">
        </div>

        <div class="file-list" id="fileList"></div>

        <button class="upload-btn" id="uploadBtn" disabled>开始上传</button>

        <div class="preview-container" id="previewContainer" style="display: none;">
            <h3 class="preview-title">图片预览</h3>
            <div class="preview-grid" id="previewGrid"></div>
        </div>
    </div>

    <script>
        const dropArea = document.getElementById('dropArea');
        const fileInput = document.getElementById('fileInput');
        const fileList = document.getElementById('fileList');
        const uploadBtn = document.getElementById('uploadBtn');
        const previewContainer = document.getElementById('previewContainer');
        const previewGrid = document.getElementById('previewGrid');

        function onClickUpload(event){
            event.stopPropagation();
            document.getElementById('fileInput').click();
        }

        let files = [];

        // 拖放事件处理
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            dropArea.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            dropArea.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            dropArea.addEventListener(eventName, unhighlight, false);
        });

        function highlight() {
            dropArea.classList.add('dragover');
        }

        function unhighlight() {
            dropArea.classList.remove('dragover');
        }

        dropArea.addEventListener('drop', handleDrop, false);
        dropArea.addEventListener('click', () => fileInput.click());

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const droppedFiles = dt.files;
            handleFiles(droppedFiles);
        }

        fileInput.addEventListener('change', function() {
            handleFiles(this.files);
        });

        function handleFiles(newFiles) {
            const fileArray = Array.from(newFiles);
            files = [...files, ...fileArray];
            updateFileList();
            updateUploadButton();
            createPreviews(fileArray);
        }

        function updateFileList() {
            fileList.innerHTML = '';
            files.forEach((file, index) => {
                const fileItem = document.createElement('div');
                fileItem.className = 'file-item';
                
                const fileExtension = file.name.split('.').pop().toUpperCase();
                
                fileItem.innerHTML = `
                    <div class="file-info">
                        <div class="file-icon">${fileExtension}</div>
                        <div class="file-details">
                            <div class="file-name">${file.name}</div>
                            <div class="file-size">${formatFileSize(file.size)}</div>
                            <div class="progress-bar">
                                <div class="progress" id="progress-${index}"></div>
                            </div>
                        </div>
                    </div>
                    <button class="remove-btn" onclick="removeFile(${index})">✕</button>
                `;
                fileList.appendChild(fileItem);
            });
        }

        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        function removeFile(index) {
            files.splice(index, 1);
            updateFileList();
            updateUploadButton();
            updatePreviews();
        }

        function updateUploadButton() {
            uploadBtn.disabled = files.length === 0;
        }

        function createPreviews(newFiles) {
            newFiles.forEach(file => {
                if (file.type.startsWith('image/')) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const previewItem = document.createElement('div');
                        previewItem.className = 'preview-item';
                        previewItem.innerHTML = `
                            <img src="${e.target.result}" class="preview-image" alt="${file.name}">
                            <button class="remove-btn" onclick="removePreview(this)">✕</button>
                        `;
                        previewGrid.appendChild(previewItem);
                        previewContainer.style.display = 'block';
                    };
                    reader.readAsDataURL(file);
                }
            });
        }

        function removePreview(btn) {
            const previewItem = btn.parentElement;
            previewGrid.removeChild(previewItem);
            if (previewGrid.children.length === 0) {
                previewContainer.style.display = 'none';
            }
        }

        function updatePreviews() {
            // 在实际应用中，这里可以根据需要更新预览
        }

        // 上传功能
        uploadBtn.addEventListener('click', uploadFiles);

        async function uploadFiles() {
            if (files.length === 0) return;

            uploadBtn.disabled = true;
            uploadBtn.textContent = '上传中...';

            for (let i = 0; i < files.length; i++) {
                await uploadFile(files[i], i);
            }

            uploadBtn.textContent = '上传完成';
            setTimeout(() => {
                uploadBtn.textContent = '开始上传';
                uploadBtn.disabled = false;
                files = [];
                updateFileList();
                previewGrid.innerHTML = '';
                previewContainer.style.display = 'none';
            }, 2000);
        }

        function uploadFile(file, index) {
            return new Promise((resolve) => {
                const formData = new FormData();
                formData.append('imgfile', file);
                // formData.append('imgfile', file.name);

                const xhr = new XMLHttpRequest();
                const progressBar = document.getElementById(`progress-${index}`);

                xhr.upload.addEventListener('progress', (e) => {
                    if (e.lengthComputable) {
                        const percentComplete = (e.loaded / e.total) * 100;
                        progressBar.style.width = percentComplete + '%';
                    }
                });

                xhr.addEventListener('load', () => {
                    if (xhr.status === 200) {
                        console.log(`文件 ${file.name} 上传成功`);
                    } else {
                        console.error(`文件 ${file.name} 上传失败`);
                    }
                    resolve();
                });

                xhr.addEventListener('error', () => {
                    console.error(`文件 ${file.name} 上传出错`);
                    resolve();
                });

                // 这里替换为你的上传接口URL
                xhr.open('POST', 'http://localhost:11223/Uploadfile/uploadimgfile', true);
                xhr.send(formData);
            });
        }
    </script>
</body>
</html>